/*
 *  Copyright (C) 2018 justlive1
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 */
layui.use(['layim', 'flow'], function () {

  var cache = []; //用于临时记录请求到的数据
  layui.$.get('checkFriendGroup', function (res) {

    if (res.code == 0) {
      layui.$.each(res.data.notifies, function (index, value) {
        var unixTimestamp = new Date(parseInt(value.timestamp));
        value.timestamp = unixTimestamp.toLocaleString();
      });
      cache = res.data.notifies;
      var tpl = layui.$("#msgbox-tpl").html();
      layui.laytpl(tpl).render(res.data, function (html) {
        layui.$("#msgbox-view").html(html);
      });
    }
  })

  //操作
  var active = {
    //同意
    agree: function (othis) {
      var li = othis.parents('li')
        , uid = li.data('uid')
        , index = li.data('index')
        , from_group = li.data('fromgroup')
        , user = cache[index].user
        , category = cache[index].type
        , notifieid = cache[index].id
        , groupid = cache[index].groupid;
      if (category == 1) {
        //选择分组
        parent.layui.layim.setFriendGroup({
          type: 'friend'
          , username: user.username
          , avatar: user.avatar
          , group: parent.layui.layim.cache().friend //获取好友分组数据
          , submit: function (group, index) {

            //将好友追加到主面板
            parent.layui.layim.addList({
              type: 'friend'
              , avatar: user.avatar //好友头像
              , username: user.username //好友昵称
              , groupid: group //所在的分组id
              , id: uid //好友ID
              , sign: user.sign //好友签名
            });
            parent.layer.close(index);
            othis.parent().html('已同意');

            layui.$.post('addFriend', {
              uid: uid //对方用户ID
              , from_group: from_group //对方设定的好友分组
              , group: group //我设定的好友分组
              , notifieid: notifieid
            }, function (res) {
              if (res.code != 0) {
                return layer.msg(res.msg);
              }
            });
          }
        });
      } else {
        //申请加群
        layui.$.get("getBevyInfo", {"id": groupid}, function (res) {
          layer.confirm('确定同意吗？', function (index) {
            $.post('addBevy', {
              notifieid: notifieid //对方用户ID
            }, function (res) {
              if (res.code != 0) {
                return layer.msg(res.msg);
              }
              layer.close(index);
              othis.parent().html('<em>已同意</em>');
            });
          });
        });
      }
    }
    //拒绝
    , refuse: function (othis) {
      var li = othis.parents('li')
        , uid = li.data('uid')
        , index = li.data('index')
        , notifieid = cache[index].id;
      layer.confirm('确定拒绝吗？', function (index) {
        layui.$.get('refuseFriend', {
          notifieid: notifieid //对方用户ID
        }, function (res) {
          if (res.code == 0) {
            layer.close(index);
            othis.parent().html('<em>已拒绝</em>');
          } else {
            layer.close(index);
            othis.parent().html('<em>未拒绝</em>');
          }
        });
      });
    }
  };

  layui.$('body').on('click', '.layui-btn', function () {
    var othis = $(this), type = othis.data('type');
    active[type] ? active[type].call(this, othis) : '';
  });
});

toLocaleString = function () {
  return this.getFullYear() + "年" + (this.getMonth() + 1) + "月" + this.getDate()
    + "日 " + this.getHours() + "点" + this.getMinutes() + "分" + this.getSeconds()
    + "秒";
};
